import path from 'path'
import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import AutoImport from 'unplugin-auto-import/vite'
import Components from 'unplugin-vue-components/vite'
import { ElementPlusResolver } from 'unplugin-vue-components/resolvers'
import Icons from 'unplugin-icons/vite'
import IconsResolver from 'unplugin-icons/resolver'

// 返回
export default defineConfig({
  // 插件
  plugins: [
    vue(),
    AutoImport({
      resolvers: [
        ElementPlusResolver({
          // 自动引入修改主题色添加这一行，使用预处理样式，
          // 不添加将会导致使用ElMessage，
          // ElNotification等组件时默认的主题色会覆盖自定义的主题色
          importStyle: 'sass'
        })
      ],
    }),
    Components({
      resolvers: [
        // ElementPlus解析规则
        ElementPlusResolver({
          // 自动引入修改主题色添加这一行，使用预处理样式，
          // 不添加将会导致使用ElMessage，
          // ElNotification等组件时默认的主题色会覆盖自定义的主题色
          importStyle: 'sass'
        }),
        // 自定义 Hsa 组件解析规则
        (name) => {
          if (name.startsWith('Hsa')) {
            return { name, from: 'hsa-ui' }
          }
          return null;
        }
      ],
    }),
    AutoImport({
      resolvers: [
        IconsResolver({
          prefix: 'Icon',
        })
      ],
    }),
    Components({
      resolvers: [
        IconsResolver({
          prefix: 'icon',
          enabledCollections: ['ep'],
        }),
      ],
    }),
    Icons({
      autoInstall: true,
    })
  ],
  // 配置
  resolve: {
    alias: {
      '@': path.resolve(__dirname, './src'),
      '~api': path.resolve(__dirname, './src/api'),
      'hsa-ui': path.resolve(__dirname, './src/framework/hsa-ui'),
      'router': path.resolve(__dirname, './src/framework/routers'),
      'app': path.resolve(__dirname, './src/framework/app'),
    },
  },
  assetsInclude: ['**/*.md'], // 添加对 md 文件的支持
  css: {
    preprocessorOptions: {
      scss: {
        additionalData: `@use "@/theme.scss" as *;`,
      },
    },
  },
  // 服务
  server: {
    open: true,
    port: 8089,
    host: '0.0.0.0', // 添加此行以允许通过 IP 访问
    proxy: {
      // 胡其耀
      '/huqiyao/web': {
        target: 'http://192.168.2.201:8788',
        rewrite: (path) => path.replace(/^\/huqiyao\/web/, '/sjzq/hsa-sjzq-base/web'),
        changeOrigin: true, // 是否跨域
        ws: false, // 是否启用 WebSockets
        secure: false, // 验证 SSL 证书
      },
      // 钟栋
      '/zongdong/web': {
        target: 'http://192.168.2.56:8788',
        rewrite: (path) => path.replace(/^\/zongdong\/web/, '/sjzq/hsa-sjzq-base'),
        // rewrite: (path) => path.replace(/^\/zongdong\/web/, '/sjzq/hsa-sjzq-base/web'),
        changeOrigin: true, // 是否跨域
        ws: false, // 是否启用 WebSockets
        secure: false, // 验证 SSL 证书
      },
      // 开发环境
      '/web': {
        target: 'http://visual.sjzq.hsip.gov.cn',
        rewrite: (path) => path.replace(/^\/web/, '/webroot/api/sjzq/hsa-sjzq-base/web'),
        changeOrigin: true, // 是否跨域
        ws: false, // 是否启用 WebSockets
        secure: false, // 验证 SSL 证书
      },
    }
  }
})
